<template>
  <div class="login">
    <div class="login-box login-style">
      <div class="top">
        <div class="logo"><img src="~@/assets/img/login-logo.png"
                               alt="">
        </div>
        <div class="sign">
          <span>@faster-framework快速开发框架</span>
        </div>

      </div>
      <div class="mid">
        <el-form :model="dataForm"
                 :rules="dataRule"
                 ref="dataForm"
                 @keyup.enter.native="dataFormSubmit()"
                 status-icon>
          <el-form-item prop="username">
            <el-input class="info"
                      v-model="dataForm.username"
                      value="admin"
                      placeholder="帐号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input class="info"
                      v-model="dataForm.password"
                      type="password"
                      value="123"
                      placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-row :gutter="20">
              <el-col :span="14">
                <el-input v-model="dataForm.code"
                          placeholder="验证码">
                </el-input>
              </el-col>
              <el-col :span="10"
                      class="login-code">
                <img :src="codeUrl"
                     width="160px"
                     @click="getCode()"
                     alt="">
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item>
            <div class="item-btn"><input type="button"
                                         value="登录"
                                         @click="dataFormSubmit()"></div>
          </el-form-item>
        </el-form>
      </div>

      <div class="bottom">Copyright © 2021 faster-framework快速开发框架</div>
    </div>
  </div>
</template>

<script>

import {Base64} from "js-base64";
import {getCodeImg, dataFormSubmit} from "../api/login"

export default {
  data() {
    return {
      dataForm: {
        username: 'admin',
        password: '123',
        code: '',
        uuid: ''
      },
      dataRule: {
        username: [
          {required: true, message: '帐号不能为空', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '验证码不能为空', trigger: 'blur'}
        ]
      },
      codeUrl: ''
    }
  },
  created() {
    this.getCode();
  },
  methods: {
    // 验证码
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          console.log(res)
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.dataForm.uuid = res.uuid;
        }
      });
    },

    // 提交表单
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // 实现 data 数据的深拷贝，改善用户体验
          const data = JSON.parse(JSON.stringify(this.dataForm));
          // data.password = Base64.encode(data.password);
          console.log("data:", data);
          dataFormSubmit(this.dataForm.username,this.dataForm.password,this.dataForm.code,this.dataForm.uuid)
              .then(res => {
                // debugger
                if (res.data.code === "00000") {
                  if (res.data.data.status === "00002") return this.$message.error("验证码失效！");
                  if (res.data.data.status === "00003") return this.$message.error("验证码错误！");
                  if (res.data.data.status === "00000") {
                    this.$message.success("登录成功！");
                    // 将信息存入sessionStorage中
                    if (!window.sessionStorage.getItem("store")) {
                      window.sessionStorage.setItem("store", JSON.stringify(res.data.data));
                    }

                    // 将 token 存入 Vuex 里面
                    this.$store.commit('setToken', res.data.data.token);
                    console.log("token:", this.$store.getters.getToken);
                    // 将 user 存入 Vuex 里面
                    this.$store.commit("setUser", res.data.data.user);
                    console.log("user:", this.$store.getters.getUser);
                    // 进行重定向
                    return this.$router.replace("/home");
                  }
                  return this.$message.error("系统异常！");
                }
                console.log("res:", res)
                return this.$message.error("账号密码错误！");
              })
              .catch(error => {
                // debugger
                this.$message.error("接口调用超时！")
                console.error(error);
              })
        }
      })
    },
  }
}
</script>

<style scoped>
.sign {
  margin-top: 10px !important;
}

.sign span {
  font-weight: bolder;
  font-family: 华文楷体;

}

.login {
  width: 100%;
  height: 100%;
  background: url(~@/assets/img/login-bg.png) no-repeat;
  background-size: cover;
  position: fixed;
}

.login .login-box {
  border-radius: 12px;
  width: 450px;
  height: 450px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  opacity: 90%;
}

.login .login-box .top {
  margin-bottom: 30px;
  text-align: center;
}

.login .login-box .top .logo {
  margin-top: 10px;
  font-size: 0;
}

.login .login-box .top .company {
  font-size: 16px;
  margin-top: 10px;
}

.login .login-box .mid {
  margin-left: 20px;
  font-size: 14px;
}

.login .login-box .mid .item-btn {
  margin-top: 20px;
  margin-right: 20px;
}

.login .login-box .mid .item-btn input {
  border: 0;
  width: 100%;
  height: 40px;
  box-shadow: 0;
  background: #1f87e8;
  color: #fff;
  border-radius: 3px;
}

.info {
  width: 410px;
}

.login-code {
  height: 40px;
}

.login .login-box .bottom {
  position: absolute;
  bottom: 10%;
  width: 100%;
  color: #999;
  font-size: 12px;
  text-align: center;
}
</style>
